﻿using MaterialDesignThemes.Wpf;
using Modules.Units;
using Newtonsoft.Json;
using Sorting.App.Controls;
using Sorting.App.Models;
using Sorting.App.Models.Enum;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using static Sorting.App.Controls.MyMessageBox;

namespace Modules.TaskMan.Views
{
    /// <summary>
    /// ViewSetSysParam.xaml 的交互逻辑
    /// </summary>
    public partial class ViewSetSysParam : UserControl
    {
        public ViewSetSysParam()
        {
            InitializeComponent();


            this.SelectScanCom.ItemsSource = GetComList();

            this.SelectAPlcCom.ItemsSource = GetPLCList();

            this.SelectBPlcCom.ItemsSource = GetPLCList();

            this.SelectSpeed.ItemsSource = GetSpeedList();




            SelectSpeed.SelectedValue = (int)AppsettingUnits.Appseeting.Gear;



            SelectAPlcCom.SelectedValue = AppsettingUnits.Appseeting.PlcPort;



            SelectScanCom.SelectedValue = (string)AppsettingUnits.Appseeting.ScanCom;


        }


        /// <summary>
        /// 串口列表
        /// </summary>
        /// <returns></returns>
        public string[] GetComList()
        {

            var res = SerialPort.GetPortNames();

            return res;
        }

        /// <summary>
        /// 获取速度列表
        /// </summary>
        /// <returns></returns>
        public int[] GetSpeedList()
        {
            int[] arr = Enumerable.Range(0, 7).ToArray();
            return arr;
        }



        /// <summary>
        /// 获取PLC列表
        /// </summary>
        /// <returns></returns>
        public int[] GetPLCList()
        {
            int[] arr = Enumerable.Range(1, 2).ToArray();
            return arr;
        }


        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Savabtn_Click(object sender, RoutedEventArgs e)
        {

            var res = File.ReadAllText(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
                "appsettings.json"));
            var json = JsonConvert.DeserializeObject<AllConfigModel>(res);

            json.CommonConfig.Gear = (EnumPlcGear)SelectSpeed.SelectedValue;
            json.CommonConfig.PlcPort = (string)SelectAPlcCom.SelectedValue;
            json.CommonConfig.ScanCom = (string)SelectScanCom.SelectedValue;


            var jsonstr = JsonConvert.SerializeObject(json);
            Newtonsoft.Json.JsonSerializer serializer = new Newtonsoft.Json.JsonSerializer();
            TextReader tr = new StringReader(jsonstr);
            JsonTextReader jtr = new JsonTextReader(tr);
            object obj = serializer.Deserialize(jtr);
            if (obj != null)
            {
                StringWriter textWriter = new StringWriter();
                JsonTextWriter jsonWriter = new JsonTextWriter(textWriter)
                {
                    Formatting = Formatting.Indented,
                    Indentation = 4,//缩进字符数
                    IndentChar = ' '//缩进字符
                };
                serializer.Serialize(jsonWriter, obj);
                jsonstr = textWriter.ToString();
            }
            File.WriteAllText(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
                "appsettings.json"), jsonstr);

            //保存后重启软件
            var result = MessageBoxHelper.ShowDialog<MessageBoxSimpleV2>(null, "设备配置信息修改！请重启！");
            if (result.Button == MessageBoxResultButton.Yes)
            {
                Process.GetCurrentProcess().Kill();
                Application.Current.Shutdown();
            }


        }

        private void PackIcon_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            DialogHost.Close("RootDialog");
        }
    }
}
